package com.example.qxfw.nyqx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.ny.CropDistIndex;
import com.example.qxfw.nyqx.service.CropDistIndexService;
import com.example.qxfw.nyqx.service.NybchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "农气预报-病虫害预报")
@RestController
@CrossOrigin
@RequestMapping("/DiseaseAndPestPredictionController")
public class DiseaseAndPestPredictionController {

    @Autowired
    private CropDistIndexService cropDistIndexService;

    @Autowired
    private NybchService nybchService;

    @ApiOperation("获取作物名称")
    @GetMapping("/getCropName")
    public SysResult getCropName() {
        List<String> cropName = cropDistIndexService.getCropName();
        return SysResult.success(cropName);
    }

    @ApiOperation("获取作物病虫害指标")
    @GetMapping("/getCropDistIndex")
    public SysResult getCropDistIndex(String cropName, String time) {
        List<CropDistIndex> cropDistIndiceList = cropDistIndexService.getList2( cropName, time);
        if (cropDistIndiceList.size()!=0){
            List<CropDistIndex> list = cropDistIndiceList.stream().map(
                    i -> {
                        CropDistIndex bo = new CropDistIndex();
                        BeanUtils.copyProperties(i, bo);
                        Calendar calendar = Calendar.getInstance();
                        int year = calendar.get(Calendar.YEAR);
                        String startTime = bo.getStarttime();
                        String a = startTime.substring(0, 2);
                        String b = startTime.substring(2);
                        startTime = year + "-" + a + "-" + b;
                        bo.setStarttime(startTime);
                        String endTime = year + "-"+ bo.getEndtime();
                        bo.setEndtime(endTime);
                        return bo;
                    }
            ).collect(Collectors.toList());
            return SysResult.success(list);
        } else {
            return SysResult.fail("未查询到数据");
        }

    }

    @ApiOperation("病虫害预报")
    @GetMapping("/ybzz")
    public SysResult ybzz(String cropName,String time) throws ParseException {
        return nybchService.ybzz(cropName,time);
    }

}
